<?php

/**
 * alltosun.com 订单widget order.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 吴松键 (wusj@alltosun.com) $
 * $Date: 2014-9-3 下午6:29:13 $
 * $Id$
 */
class order_widget
{
    private $per_page = 10;
    private $page_no = 1;

    /**
     * 获取支付方式列表
     */
    public function get_pay_type_list($cur_user_info) {
        $list = array();
        foreach (order_config::$pay_type_list as $k => $v) {
            $cur_pay_type = array();
            $cur_pay_type['pay_type'] = $k;
            $cur_pay_type['pay_type_str'] = $v;
            if (isset(order_config::$allow_pay_type_list[$k])) {
                $cur_pay_type['is_allow'] = 1;
            } else {
                $cur_pay_type['is_allow'] = 0;
            }
            if (isset(order_config::$allow_show_pay_type_list[$k])) {
                $cur_pay_type['is_allow_show'] = 1;
            } else {
                $cur_pay_type['is_allow_show'] = 0;
            }
            if ($k == 1) {
                // 货到付款,获取默认仓库地址
                $dealer_address = dealers_helper::get_default_dealer_address($cur_user_info['id']);
                if ($dealer_address) {
                    $cur_pay_type['pay_type_con'] = '地址: ' . $dealer_address['dealer_address'];
                } else {
                    $cur_pay_type['pay_type_con'] = '地址: ';
                }
            } elseif ($k == 2) {
                // 在线支付 支持微信支付宝支付
                $cur_pay_type['pay_type_con'] = '支持微信支付宝支付';
            } elseif ($k == 3) {
                // 打白条 支持赊欠贷款
                $cur_pay_type['pay_type_con'] = '支持赊欠贷款';
            }

            $list[] = $cur_pay_type;
        }

        return $list;
    }

    /**
     * TODO 合伙人确认到货迁移到 order/controller/operation.php, 下个挨刀的就是门店订单, 洗干净等着吧
     *
     * 修改订单状态-确认到货
     * @param unknown $order_id
     * @param number $receiver_type 1=>合伙人确认, 2=>助理确认, 3=>自动确认
     * @param string $receiver_extra 确认备注
     * @param unknown $cur_user_info
     * @return multitype:boolean string
     */
    public function confirm_order_progress($order_id, $receiver_type = 1, $receiver_extra = '', $cur_user_info = array()) {
        $progress = 10;

        if (!$order_id) {
            return array('flag' => false, 'info' => '订单ID无效');
        }

        if (!$receiver_type || !in_array($receiver_type, array(1, 2, 3))) {
            return array('flag' => false, 'info' => '确认到货类型错误');
        }

        if (2 == $receiver_type) {
            if (!$receiver_extra) {
                return array('flag' => false, 'info' => '助理确认到货须填写确认到货备注');
            }
        }

        $order_decode_id = order_helper::order_id_decode($order_id);

        if (!is_numeric($order_decode_id)) {
            api_helper::return_data(1, $order_decode_id);
        }

        $order_info = _uri('order', $order_decode_id);

        if (!$order_info || !$order_info['status']) {
            return array('flag' => false, 'info' => '订单不存在或者已经删除');
        }

        $store_id = $order_info['store_id'];

        if (!$store_id) {
            return array('flag' => false, 'info' => '商户ID无效');
        }

        $store_info = _uri('store', $store_id);

        if (!$store_info || $store_info['status'] == 0) {
            return array('flag' => false, 'info' => '商户不存在或者已删除');
        }

        if (!$cur_user_info) {
            // 如果当前信息不存在 默认是商户信息
            $cur_user_info = user_helper::get_user_info($store_info['user_id']);
        }
        $cur_user_id = $cur_user_info['id'];

        // if($order_info['store_id'] != $store_id || $order_info['user_id'] != $cur_user_id) {
        //     api_helper::return_data(1, '订单不属于您的商户');
        // }

        if ($store_info['user_id'] != $cur_user_id &&
            $store_info['owner_id'] != $cur_user_id &&
            $store_info['agency_id'] != $cur_user_id &&
            $order_info['assigned_id'] != $cur_user_id
        ) {
            return array('flag' => false, 'info' => '此订单您无权限操作');
        }
        //查询经销商信息
        $agency_store_info = _uri('store', array('user_id' => $store_info['agency_id']));
        $agency_user_store_info = _uri('user_store', array('user_id' => $store_info['agency_id']));

        if ($progress == $order_info['progress']) {
            return array('flag' => false, 'info' => '请勿重复修改同一状态');
        }

        //只允许订单确认收货和取消订单
        if ($store_info['user_id'] == $cur_user_id || $store_info['owner_id'] == $cur_user_id) {
            $update_progress_list = array(10, 15);
        } else {
            $update_progress_list = array(5, 10, 15);
        }

        if (!in_array($progress, $update_progress_list)) {
            return array('flag' => false, 'info' => '非法操作订单状态');
        }

        if ($order_info['progress'] != 5) {
            return array('flag' => false, 'info' => '订单无法确认收货');
        }
        $info = array(
            'progress' => $progress,
            'receiver_time' => date('Y-m-d H:i:s')
        );

        $affected = _model('order')->update($order_info['id'], $info);
        $store_user_info = _uri('user', $store_info['user_id']);
        $agency_user_info = _uri('user', $store_info['agency_id']);

        //xiaolin 增加可开发票额度(合伙人给掌柜的开票额度)
        //合伙人向总部下的订单
        if ($order_info['type'] == 1) {
            invoice_helper::change_user_invoice_amount($cur_user_id, $order_info['price'], 1, 1);
        }

        if ($affected) {
            // 更新分支表
            $order_branch_info = array(
                'receiver_type' => $receiver_type,
                'receiver_extra' => $receiver_extra,
            );
            $branch_affected = _model('order_branch')->update(array('order_id' => $order_info['id']), $order_branch_info);

            // 终端确认收货信息处理
            if ($order_info['type'] == 5) {
                _widget('order')->confirm_order_arrival($cur_user_info, $store_id, $store_info, $order_info, $agency_store_info, $agency_user_store_info, $agency_user_info, $order_id);
            } elseif ($order_info['type'] == 1) {
                // 经销商确认收货
                _widget('dealers')->confirm_order_arrival($store_user_info, $store_id, $store_info, $order_info, $order_id);
            }
            return array('flag' => true, 'info' => 'success');
        } else {
            api_helper::return_data(1, '网络原因，请重试');
            return array('flag' => false, 'info' => '网络原因，请重试');
        }
    }

    /**
     * 修改订单状态-撤销订单
     * @param unknown $order_id
     * @param string $reason
     * @param unknown $cur_user_info
     * @return multitype:boolean string
     */
    public function cancel_order_progress($order_id, $reason = '', $cur_user_info = array()) {
        $progress = 15;

        if (!$order_id) {
            return array('flag' => false, 'info' => '订单ID无效');
        }

        $order_decode_id = order_helper::order_id_decode($order_id);

        if (!is_numeric($order_decode_id)) {
            api_helper::return_data(1, $order_decode_id);
        }

        $order_info = _uri('order', $order_decode_id);

        if (!$order_info || !$order_info['status']) {
            return array('flag' => false, 'info' => '订单不存在或者已经删除');
        }

        $store_id = $order_info['store_id'];

        if (!$store_id) {
            return array('flag' => false, 'info' => '商户ID无效');
        }

        $store_info = _uri('store', $store_id);

        if (!$store_info || $store_info['status'] == 0) {
            return array('flag' => false, 'info' => '商户不存在或者已删除');
        }

        if (!$cur_user_info) {
            // 如果当前信息不存在 默认是商户信息
            $cur_user_info = user_helper::get_user_info($store_info['user_id']);
        }
        $cur_user_id = $cur_user_info['id'];

        // if($order_info['store_id'] != $store_id || $order_info['user_id'] != $cur_user_id) {
        //     api_helper::return_data(1, '订单不属于您的商户');
        // }

        if ($store_info['user_id'] != $cur_user_id &&
            $store_info['owner_id'] != $cur_user_id &&
            $store_info['agency_id'] != $cur_user_id &&
            $order_info['assigned_id'] != $cur_user_id
        ) {
            return array('flag' => false, 'info' => '此订单您无权限操作');
        }
        //查询经销商信息
        $agency_store_info = _uri('store', array('user_id' => $store_info['agency_id']));
        $agency_user_store_info = _uri('user_store', array('user_id' => $store_info['agency_id']));

        if ($progress == $order_info['progress']) {
            return array('flag' => false, 'info' => '请勿重复修改同一状态');
        }

        //只允许订单确认收货和取消订单
        if ($store_info['user_id'] == $cur_user_id || $store_info['owner_id'] == $cur_user_id) {
            $update_progress_list = array(10, 15);
        } else {
            $update_progress_list = array(5, 10, 15);
        }

        if (!in_array($progress, $update_progress_list)) {
            return array('flag' => false, 'info' => '非法操作订单状态');
        }

        if (!$reason) {
            return array('flag' => false, 'info' => '请填写取消订单的原因');
        }
        if ($order_info['progress'] != 1) {
            return array('flag' => false, 'info' => '无法取消订单');
        }
        $info = array(
            'progress' => $progress,
            'cancel_order_user_id' => $cur_user_id,
            'cancel_order_time' => date('Y-m-d H:i:s'),
            'reason' => $reason,
        );

        $affected = _model('order')->update($order_info['id'], $info);
        $store_user_info = _uri('user', $store_info['user_id']);
        $agency_user_info = _uri('user', $store_info['agency_id']);

        if ($affected) {

            // TODO pay 取消订单, 退回已支付款项
            order_helper::cancel_order_send_back($order_info['id'], $reason);

            if ($order_info['type'] == 5) {
                $order_info['order_id'] = $order_id;
                _widget('order')->cancel_order($cur_user_info, $store_info, $order_info);
            } elseif ($order_info['type'] == 1) {
                $order_info['order_id'] = $order_id;
                _widget('dealers.message')->cancel_order_message($cur_user_info, $order_id);
            } else {

            }
            return array('flag' => true, 'info' => 'success');
        } else {
            api_helper::return_data(1, '网络原因，请重试');
            return array('flag' => false, 'info' => '网络原因，请重试');
        }
    }

    /**
     * 修改订单状态-确认发货
     */
    public function deliver_order_progress($order_id, $cur_user_info = array()) {
        $progress = 5;

        if (!$order_id) {
            return array('flag' => false, 'info' => '订单ID无效');
        }

        $order_decode_id = order_helper::order_id_decode($order_id);

        if (!is_numeric($order_decode_id)) {
            api_helper::return_data(1, $order_decode_id);
        }

        $order_info = _uri('order', $order_decode_id);

        if (!$order_info || !$order_info['status']) {
            return array('flag' => false, 'info' => '订单不存在或者已经删除');
        }

        $store_id = $order_info['store_id'];

        if (!$store_id) {
            return array('flag' => false, 'info' => '商户ID无效');
        }

        $store_info = _uri('store', $store_id);

        if (!$store_info || $store_info['status'] == 0) {
            return array('flag' => false, 'info' => '商户不存在或者已删除');
        }

        if (!$cur_user_info) {
            // 如果当前信息不存在 默认是商户信息
            $cur_user_info = user_helper::get_user_info($store_info['user_id']);
        }
        $cur_user_id = $cur_user_info['id'];

        // if($order_info['store_id'] != $store_id || $order_info['user_id'] != $cur_user_id) {
        //     api_helper::return_data(1, '订单不属于您的商户');
        // }

        if ($store_info['user_id'] != $cur_user_id &&
            $store_info['owner_id'] != $cur_user_id &&
            $store_info['agency_id'] != $cur_user_id &&
            $order_info['assigned_id'] != $cur_user_id
        ) {
            return array('flag' => false, 'info' => '此订单您无权限操作');
        }
        //查询经销商信息
        $agency_store_info = _uri('store', array('user_id' => $store_info['agency_id']));
        $agency_user_store_info = _uri('user_store', array('user_id' => $store_info['agency_id']));

        $progress_list = order_config::$store_progress;
        if (!$progress || !$progress_list[$progress]) {
            return array('flag' => false, 'info' => '状态值无效');
        }

        if ($progress == $order_info['progress']) {
            # @wangdk 为了兼容低版本的错误
            # return array('flag'=>false, 'info'=>'请勿重复修改同一状态');
            return array('flag' => true, 'info' => 'success');
        }

        //只允许订单确认收货和取消订单
        if ($store_info['user_id'] == $cur_user_id || $store_info['owner_id'] == $cur_user_id) {
            $update_progress_list = array(10, 15);
        } else {
            $update_progress_list = array(5, 10, 15);
        }

        if (!in_array($progress, $update_progress_list)) {
            return array('flag' => false, 'info' => '非法操作订单状态');
        }

        if ($order_info['progress'] != 1) {
            return array('flag' => false, 'info' => '订单状态无效，无法进行发货');
        }
        $info = array(
            'progress' => $progress,
            'deliver_time' => date('Y-m-d H:i:s')
        );

        $affected = _model('order')->update($order_info['id'], $info);
        $store_user_info = _uri('user', $store_info['user_id']);
        $agency_user_info = _uri('user', $store_info['agency_id']);

        if ($affected) {
            if ($order_info['type'] == 5) {
                _widget('order')->transport_order($store_info, $order_info);
            } elseif ($order_info['type'] == 1) {

            } else {

            }

            return array('flag' => true, 'info' => 'success');
        } else {
            api_helper::return_data(1, '网络原因，请重试');
            return array('flag' => false, 'info' => '网络原因，请重试');
        }
    }


    /**
     * 更新订单状态
     * @param unknown $cur_user_info 如果是后台确认则$cur_user_info 为商家用户信息
     * @param unknown $order_id 订单号
     * @param unknown $progress 10=>确认收货 15是撤销订单 默认是10
     * @param unknown $reason 如果是撤销订单可以传撤销订单的原因
     * @return multitype:boolean string
     * @author gaoa@alltosun.com
     */
    public function update_order_progress($order_id, $progress = 10, $reason = '', $cur_user_info = array()) {
        if ($progress == 10) {
            return _widget('order')->confirm_order_progress($order_id, 1, '', $cur_user_info);
        } elseif ($progress == 15) {
            return _widget('order')->cancel_order_progress($order_id, $reason, $cur_user_info);
        } elseif ($progress == 5) {
            return _widget('order')->deliver_order_progress($order_id, $cur_user_info);
        } else {
            return array('flag' => false, 'info' => '订单状态无效');
        }
    }

    /**
     * 修改订单是否收款的状态
     * @param unknown $order_id
     * @param number $is_pay
     */
    public function change_order_pay_status($user_id, $order_id, $is_pay = 1) {
        if (!$order_id || !is_numeric($order_id)) {
            return array('flag' => false, 'info' => '订单编码错误');
        }

        $order_info = order_helper::get_order_info($order_id);
        if (!$order_info) {
            return array('flag' => false, 'info' => '此订单不存在');
        }

        if (1 == $order_info['type']) {
            $role_id = user_helper::get_user_role_id($user_id);
            $allow_user = array(user_config::ADMIN, user_config::FINANCIAL_MANAGEMENT, user_config::ASSISTANT);
            if (!in_array($role_id, $allow_user)) {
                return array('flag' => false, 'info' => '没有此操作权限');
            }
        } elseif (5 == $order_info['type']) {
            if ($order_info['agency_id'] != $user_id && $order_info['assigned_id'] != $user_id) {
                return array('flag' => false, 'info' => '没有此操作权限');
            }
            $role_id = user_helper::get_user_role_id($user_id);
            if ($role_id != user_config::AGENCY && $role_id != user_config::MARKETING) {
                return array('flag' => false, 'info' => '没有此操作权限');
            }
        }

        // @TODO 是否有订单状态的限制
        $allow_progress = array(10);
        if (!in_array($order_info['progress'], $allow_progress)) {
            return array('flag' => false, 'info' => '此订单在当前状态下禁止修改');
        }

        if ($order_info['is_pay'] == $is_pay) {
            return array('flag' => false, 'info' => '请勿重复修改订单状态');
        }

        $affected_row = _model('order')->update($order_id, array('is_pay' => $is_pay));
        if ($affected_row) {
            return array('flag' => true, 'info' => '修改成功');
        } else {
            return array('flag' => false, 'info' => '网络原因请重试');
        }

    }

    /**
     * 处理订单，确认收货
     * 门店终端专用
     *
     * @param unknown_type $store_id 店铺ID
     * @param unknown_type $store_info 店铺的信息
     * @param unknown_type $order_info 店铺的订单信息
     * @param unknown_type $agency_store_info 经销商的店铺信息
     * @param unknown_type $agency_user_info 经销商的用户信息
     * @param unknown_type $order_id 订单ID
     */
    public function confirm_order_arrival($user_info, $store_id, $store_info, $order_info, $agency_store_info, $agency_user_store_info, $agency_user_info, $order_id) {
        // 当前订单的积分数
        $return_score_data = _widget('score_record')->format_store_order_score($store_id, $order_info['goods_num'], $order_info['price']);

        // 收货成功增加积分和发消息
        $return_status = _widget('score_record')->change_score($store_info['user_id'], $store_id, $order_info['id'], $return_score_data['score']);

        // 增加销量、减少库存
        $order_relation_list = _model('order_relation')->getList(array('order_id' => $order_info['id'], 'status' => 1));
        foreach ($order_relation_list as $key => $value) {
            goods_helper::add_sales($agency_user_store_info['id'], $value['sku_id'], $value['goods_id'], $value['goods_num']);
            goods_helper::subtract_stock($agency_user_store_info['id'], $value['sku_id'], $value['goods_id'], $value['goods_num']);
        }

        // 增加销量记录
        _widget('order')->add_sale_record($order_info['id'], $store_id, $return_score_data['score']);

        // 减杯子的库存
        cup_helper::less_cup_num($order_info['id'], $agency_store_info['id']);

        $marketing_user_info = user_helper::get_user_info($order_info['assigned_id']);
        $owner_user_info = user_helper::get_user_info($store_info['owner_id']);

        // 第三方人员信息
        $person_info = array();
        if (isset($order_info['other_assigned_name']) && $order_info['other_assigned_name']) {
            $person_info['other_assigned_mobile'] = $order_info['other_assigned_mobile'];
            $person_info['other_assigned_name'] = $order_info['other_assigned_name'];
        }
        // 更新订单时间
        order_helper::update_order_time($order_info['id'], 'receiver_time');

        // TODO pay 返还除了现金以外的其他支付种类, 兑换/积分等
        order_helper::confirm_store_order_send_addition($order_info['id']);

        _widget('order.message')->confirm_finished_message($user_info, $store_info, $marketing_user_info, $agency_user_info, $person_info, $order_id);
    }

    /**
     * 撤销订单
     * @param $cur_user_info 当前登录用户信息
     * @param $store_info 店铺信息
     */
    public function cancel_order($cur_user_info, $store_info, $order_info) {
        // 更新订单时间
        order_helper::update_order_time($order_info['id'], 'cancel_order_time');

        $marketing_info = array();
        $dealer_info = array();
        $person_info = array();
        if ($store_info['marketing_id'] && $store_info['marketing_id'] != $store_info['agency_id']) {
            if ($order_info['assigned_id']) {
                $marketing_info = user_helper::get_user_info($order_info['assigned_id']);
                $marketing_info['name'] = $marketing_info['realname'];
            } else {
                $marketing_info = user_helper::get_user_info($store_info['marketing_id']);
                $marketing_info['name'] = $marketing_info['realname'];
            }

        }

        if ($store_info['agency_id']) {
            $dealer_info = user_helper::get_user_info($store_info['agency_id']);
            $dealer_info['name'] = $dealer_info['realname'];
        }

        if ($store_info['user_id']) {
            $store_user_info = user_helper::get_user_info($store_info['user_id']);
            $store_info['name'] = $store_user_info['realname'];
            $store_info['mobile'] = $store_user_info['mobile'];
            $store_info['id'] = $store_user_info['id'];
        }
        // $order_info['order_id'] = $order_id;

        // 发送短信
        _widget('order.message')->cancel_order($cur_user_info, $store_info, $marketing_info, $dealer_info, $person_info, $order_info);
    }

    /**
     * 获取订单列表
     */
    public function get_list($params = array()) {
        $this->page_no = Request::Get('page_no', 1);
        $user_id = user_helper::get_user_id();

        if (!$user_id) {
            return array();
        }

        if (isset($params['per_page']) && $params['per_page']) {
            $this->per_page = $params['per_page'];
        }
        if (isset($params['page_no']) && $params['page_no']) {
            $this->page_no = $params['page_no'];
        }
        if (isset($params['order_id']) && $params['order_id']) {
            $filter['id'] = $params['order_id'];
        }
        if (isset($params['progress']) && $params['progress']) {
            $filter['progress'] = $params['progress'];
        }
        $filter['user_id'] = $user_id;
        $filter['status'] = 1;

        $list = array();
        $count = _model('order')->getTotal($filter);
        if ($count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($count)) {
                Response::assign('pager', $pager);
            }

            if ($pager->getPages() >= $this->page_no) {
                $list = _model('order')->getList($filter, ' ORDER BY `add_time` DESC ' .
                                                          $pager->getLimit($this->page_no));
                foreach ($list as $k => $v) {
                    /*
                    $progress = order_helper::auto_status($v['id']);
                    if ($filter['progress'] && $progress != $v['progress']) {
                        unset($list[$k]);
                    } else {
                        $list[$k]['progress'] = $progress;
                    }
                    */
                    $list[$k]['order_code'] = order_helper::order_id_encode($v['id']);
                    $list[$k]['detail'] = order_helper::get_order_relation_by_order_id($v['id']);
                }
            }
        }

        return array_values($list);
    }


    /**
     * 获取商品订单详情
     *
     */
    public function get_order_detail($order_info, $store_id, $addon_price) {
        if (!$order_info || !$store_id || !$addon_price) {
            return array();
        }

        $order_relation_list = _model('order_relation')->getList(array('order_id' => $order_info['id'], 'status' => 1));
        if ($order_relation_list) {
            foreach ($order_relation_list as $key => $value) {
                $goods_info = _uri('goods', $value['goods_id']);
                if ($goods_info['cover']) {
                    $cover_path = _uri('attachment', $goods_info['cover'], 'path');
                    $goods_info['cover'] = _image($cover_path, 'list');
                }
                $order_relation_list[$key]['goods_info'] = $goods_info;
                //                if ($order_info['type'] == 5) {
                //                    $order_relation_list[$key]['goods_info']['sku_list'] = goods_helper::get_old_store_sku_list($store_id, $goods_info['id'], $order_info['id']);
                //                } else {
                //                    $order_relation_list[$key]['goods_info']['sku_list'] = goods_helper::discard_get_old_store_sku_list_user_order_detail($store_id, $goods_info['id'], $order_info['id']);
                //                }
            }
        }

        if (!$order_relation_list) {
            return array();
        }

        $order_info['order_relation_list'] = $order_relation_list;
        //添加杯型数据
        $order_cup_list = _model('order_cup')->getList(array('order_id' => $order_info['id']));
        $order_info['order_cup_list'] = $order_cup_list;

        // 兑换数据 shinate 2016-08-02
        $order_exchange_list = _model('order_exchange_relation')->getList(array(
            'order_id' => $order_info['id'],
            'status' => 1
        ));
        if ($order_exchange_list) {
            foreach ($order_exchange_list as $key => $value) {
                $goods_info = _uri('goods', $value['goods_id']);
                if ($goods_info['cover']) {
                    $cover_path = _uri('attachment', $goods_info['cover'], 'path');
                    $goods_info['cover'] = _image($cover_path, 'list');
                }
                $order_exchange_list[$key]['goods_info'] = $goods_info;
                //$order_exchange_list[$key]['goods_info']['sku_list'] = goods_helper::get_store_sku_list($store_id, $goods_info['id']);
            }
        }

        $order_info['order_exchange_list'] = $order_exchange_list;

        //订单的积分
        // $order_info['score'] = score_record_helper::get_order_score($store_info['user_id'], $order_info['id']);

        // 订单支付凭证信息
        if (!$order_info['payoff_person']) {
            $order_info['payoff_person'] = '暂无';
        }
        if (!$order_info['payoff_money'] || $order_info['payoff_money'] == '0.0') {
            $order_info['payoff_money'] = '暂无';
        } else {
            $order_info['payoff_money'] .= '';
        }
        if (!$order_info['payoff_extra']) {
            $order_info['payoff_extra'] = '暂无';
        }
        if (!$order_info['payoff_time'] || $order_info['payoff_time'] == '0000-00-00 00:00:00') {
            $order_info['payoff_time'] = '暂无';
        }

        //对订单ID进行编码
        $order_info['id'] = order_helper::order_id_encode($order_info['id']);

        return $order_info;
    }


    /**
     * TODO 无用! 即将废弃
     * 自动更新订单状态
     * @param  array $params [参数]
     * @return [type]         [更新状态，无返回]
     */
    public function auto_order_status($params = array()) {
        $pay_time = 1;     // 付款时间，单位天
        $deliver_time = 10;    // 确认收货时间，单位天
        $order_status = 0;
        if (isset($params['pay_time']) && $params['pay_time']) {
            $pay_time = $params['pay_time'];
        }
        if (isset($params['deliver_time']) && $params['deliver_time']) {
            $deliver_time = $params['deliver_time'];
        }
        if (isset($params['order_status']) && $params['order_status']) {
            $order_status = $params['order_status'];
        }

        // 取消过期订单
        if (0 == $order_status || 1 == $order_status) {
            $filter = array(
                '`add_time` <=' => date('Y-m-d H:i:s', time() - 3600 * 24 * $pay_time),
                'progress' => 1
            );
            $count = _model('order')->getTotal($filter);
            if ($count) {
                $list = _model('order')->getList($filter);
                foreach ($list as $k => $v) {
                    _model('order')->update(array('id' => $v['id']), array('progress' => 0));
                    order_helper::refund_stock($v['id']); //返回库存
                    // 调用交易关闭接口
                    order_helper::alipay_close_trade($v['id']);
                }
            }
        }
        if (0 == $order_status || 15 == $order_status) {
            $filter = array(
                '`deliver_time` <=' => date('Y-m-d H:i:s', time() - 3600 * 24 * $deliver_time),
                'progress' => 15
            );
            $count = _model('order')->getTotal($filter);
            if ($count) {
                $list = _model('order')->getList($filter);
                foreach ($list as $k => $v) {
                    _model('order')->update(array('id' => $v['id']), array(
                        'progress' => 20,
                        'receiver_time' => date('Y-m-d H:i:s'),
                        'express_receiver_time' => date('Y-m-d H:i:s'),
                    ));
                }
            }
        }
    }

    /**
     * 记录销量库存
     * @param [type] $order_id [description]
     * @param [type] $store_id [description]
     * @param [type] $score    [description]
     */
    public function add_sale_record($order_id, $store_id, $score) {
        if (!$order_id || !$store_id) {
            return false;
        }

        $order_info = order_helper::get_order_info($order_id);

        $store_info = store_helper::get_store_info($store_id);

        if (!$store_info || !$order_info) {
            return false;
        }

        if (!$score) {
            return false;
        }

        $info = array(
            'user_id' => $store_info['user_id'],
            'store_id' => $store_info['id'],
            'order_id' => $order_info['id'],
            'box' => $order_info['goods_num'],
            'price' => $order_info['price'],
            'score' => $score
        );
        $dealer_info = $marketing_info = $info;

        $dealer_info['dealer_id'] = $store_info['agency_id'];
        $dealer_info['owner_id'] = $store_info['owner_id'];
        _model('dealer_sale_record')->create($dealer_info);

        $marketing_info['marketing_id'] = $store_info['marketing_id'];
        _model('marketing_sale_record')->create($marketing_info);

        return true;
    }

    /**
     * 初始化库存
     * @param  [type] $init_goods_list [description]
     * @param  [type] $store_id        [description]
     * @return [type]                  [description]
     */
    public function init_stock($init_goods_list, $store_id, $init_cup_list, $desc) {
        if (!$init_goods_list) {
            return array('info' => 'error', 'message' => '未提交初始化库存商品');
        }

        if (!$init_cup_list) {
            return array('info' => 'error', 'message' => '未提交初始化杯子数量');
        }

        if (!$store_id) {
            return array('info' => 'error', 'message' => '商铺ID不正确');
        }

        $store_info = store_helper::get_store($store_id);

        if (!$store_info['is_init']) {
            $desc = '首次盘点';
        }

        // 首次盘点库存不需要备注
        if (!$desc && $store_info['is_init']) {
            return array('info' => 'error', 'message' => '请填写盘点备注');
        }

        // 保初始化商品的数据
        $this->init_save_sku($init_goods_list, $store_id, $store_info);

        // 保存初始化杯子数据
        cup_helper::init_store_cup($store_id, $init_cup_list);

        // 更新初始化完毕的状态
        store_helper::update_init_store($store_id);
        $this->save_check_stock_record($store_id, $init_goods_list, $init_cup_list, $desc);

        return array('info' => 'success');

        $result = $this->init_save_sku($init_goods_list, $store_id, $store_info);
        if ($result) {
            // 保存初始化杯子数据
            if (cup_helper::init_store_cup($store_id, $init_cup_list)) {

                // 更新初始化完毕的状态
                store_helper::update_init_store($store_id);
                $this->save_check_stock_record($store_id, $init_goods_list, $init_cup_list, $desc);
                return array('info' => 'success');
            }
        }
        return array('info' => 'error', 'message' => '网络原因，初始化失败');
    }

    /**
     * @author wangdk
     * @param unknown_type $store_id 店铺ID
     * @param unknown_type $init_goods_list 商品数据
     * @param unknown_type $init_cup_list 杯子数据
     * @param unknown_type $desc 备注
     */
    private function save_check_stock_record($store_id, $init_goods_list, $init_cup_list, $desc) {
        $data['desc'] = $desc;
        $data['cup_data'] = json_encode($init_cup_list);
        $data['goods_data'] = json_encode($init_goods_list);
        $data['store_id'] = $store_id;

        return _model('check_stock_record')->create($data);
    }

    /**
     * @wangdk
     * 经销商库存初始化保存sku相关信息
     * @param unknown_type $init_goods_list
     * @param unknown_type $store_id
     * @param unknown_type $store_info
     * @return boolean|Ambigous <boolean, [type]>
     */
    private function init_save_sku($init_goods_list, $store_id, $store_info) {
        $is_ok = false;
        if (!$init_goods_list) {
            return false;
        }
        foreach ($init_goods_list as $key => $value) {

            $store_goods_info = _uri('store_goods', array('goods_id' => $value['goods_id'], 'store_id' => $store_id));
            $goods_info = _uri('goods', $value['goods_id']);

            // 看商户库存里面有没有这个商品，如果没有就添加一条
            if (!$store_goods_info) {
                $info = array(
                    'user_id' => $store_info['user_id'],
                    'goods_id' => $value['goods_id'],
                    'store_id' => $store_id,
                    'status' => 1
                );
                $store_goods_id = goods_helper::create_store_goods($info);
            }
            // 获取总店信息
            $dealers_user_store_info = store_helper::get_user_store($store_info['owner_id']);
            // 查询总店sku
            $store_info_sku_info = _uri('store_goods_sku', array(
                'sku_id' => $value['sku_id'],
                'store_id' => $dealers_user_store_info['id'],
                'goods_id' => $value['goods_id']
            ));

            //看下商户库存里面有没有这个商品的sku信息，如果有的话增加库存并上架，如果没有插入一条并上架
            if ($store_info_sku_info) {
                // @wangdk 盘点由增加库存修改为更新库存
                if (goods_helper::update_store_goods_sku($store_info_sku_info['id'], ' SET `stock` = ' .
                                                                                     $value['goods_num'])
                ) {
                    $is_ok = true;
                }
            } else {
                $info = array(
                    'user_id' => $store_info['user_id'],
                    'goods_id' => $value['goods_id'],
                    'store_id' => $dealers_user_store_info['id'],
                    'sku_id' => $value['sku_id'],
                    'price' => $goods_info['market_price'],
                    'market_price' => $goods_info['market_price'],
                    'stock' => $value['goods_num'],
                    'sale_count' => 0,
                    'status' => 1
                );
                $is_ok = goods_helper::create_store_goods_sku($info);
            }
        }

        return $is_ok;
    }

    /**
     * 第一次订单处理
     */
    public function first_order_handle($store_id) {

    }

    /**
     * 订单发货
     */
    public function transport_order($store_info, $order_info) {
        $store_owner_info = user_helper::get_user_info($store_info['owner_id']);
        $store_user_info = user_helper::get_user_info($store_info['user_id']);

        if (!$store_owner_info) {
            api_helper::return_data(1, '总店信息为找到!');
        }

        $agency_info = user_helper::get_user_info($store_info['agency_id']);
        if (!$agency_info) {
            api_helper::return_data(1, '经销商信息未找到!');
        }

        $marketing_info = user_helper::get_user_info($order_info['assigned_id']);
        /* if (!$marketing_info) {
         // api_helper::return_data(1, '市场人员信息未找到!');
        } */

        // 经销商安排发货
        $order_info['id'] = order_helper::order_id_encode($order_info['id']);
        $sms_store_info['owner_mobile'] = $store_owner_info['mobile'];
        $sms_store_info['mobile'] = $store_user_info['mobile'];
        $sms_store_info['title'] = $store_info['title'] . $store_info['title_branch'];
        $sms_store_info['user_id'] = $store_info['user_id'];

        $sms_market_info = array();
        if ($marketing_info) {
            $sms_market_info['mobile'] = $marketing_info['mobile'];
            $sms_market_info['marketing_name'] = $marketing_info['realname'];
        }

        $sms_agency_info['mobile'] = $agency_info['mobile'];
        $sms_agency_info['user_id'] = $agency_info['id'];

        $sms_person_info = array();
        if (isset($order_info['other_assigned_name']) && $order_info['other_assigned_name']) {
            $sms_person_info['other_assigned_name'] = $order_info['other_assigned_name'];
            $sms_person_info['other_assigned_mobile'] = $order_info['other_assigned_mobile'];
        }

        if ($order_info['assigned_id'] == $order_info['agency_id'] || $order_info['other_assigned_name']) {
            _widget('order.order_message')->transport_order_message(1, $order_info, $sms_store_info, $sms_market_info, $sms_agency_info, $sms_person_info);
        } else {
            _widget('order.order_message')->transport_order_message(0, $order_info, $sms_store_info, $sms_market_info, $sms_agency_info, $sms_person_info);
        }
    }

}

?>